﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using UserLoging.Database;
using UserLoging.Dtos;
using UserLoging.Models;

namespace UserLoging.Services
{
    public class UserRepository : IUserRepository
    {
        private UserDbContext _userDbContext;
        public UserRepository(UserDbContext userDbContext) 
        {
            _userDbContext = userDbContext;
        }

        public void AddUser(User user)
        {
            _userDbContext.User.Add(user);
            
        }

        public void DeleteUser(User user)
        {
            _userDbContext.Entry(user).State = EntityState.Deleted;
        }

        public User EditUser(User use, EditUserFindDto editUserFindDto)
        {
            use.password = editUserFindDto.password;
            use.number = editUserFindDto.number;
            _userDbContext.Entry(use).State = EntityState.Modified;
            return use;
        }

        public async Task<IEnumerable<User>> GetAllUser()
        {
            return await _userDbContext.User.Where(u => (int)(u.Permission)>=0).ToListAsync() ;
        }

        public int GetPerMissionNumber(string permission)
        {
            switch (permission)
            {
                case "admin":
                    {
                        return 0;
                    }
                case "user":
                    {
                        return 1;
                    }
                default:
                    {
                        return -1;
                    }
            }
        }

        public async Task<UserIncludeTotapage> GetUserByFindUsersinformationDtoAsync(GetFindUsersinformationDto findUsersinformationDto)
        {
            IEnumerable<User> allUsers;
            if (!String.IsNullOrWhiteSpace(findUsersinformationDto.Query))
            {
                allUsers = await _userDbContext.User.Where(u => u.UserName.Contains(findUsersinformationDto.Query)).OrderBy(u => u.UserId).ToListAsync();
            }
            else 
            {
               allUsers=  await _userDbContext.User.Where(u => true ).OrderBy(u => u.UserId).ToListAsync();
            }
            int totalpage = allUsers.Count();
            UserIncludeTotapage userIncludeTotapage = new UserIncludeTotapage()
            {
                Users = allUsers.Skip((findUsersinformationDto.Pagenum - 1) * findUsersinformationDto.Pagesize).Take(findUsersinformationDto.Pagesize),
                Totapage = totalpage
            };

            return userIncludeTotapage;
        }

        public async Task<User> GetUserByUserIdAsync(Guid id)
        {
            return await _userDbContext.User.SingleOrDefaultAsync(u => u.UserId == id);
        }

        public async Task<User> GetUserByUserLoginDtoAsync(UserLogingDto userLogingDto)
        {
            User use =await _userDbContext.User.SingleOrDefaultAsync(u => u.UserName == userLogingDto.UserName&&u.password==userLogingDto.Password);
            return use;
        }

        public async Task<bool> SaveAsync()
        {
            return (await _userDbContext.SaveChangesAsync() >= 0);
        }

        public void UpdataUser(User user)
        {
            _userDbContext.Entry(user).State = EntityState.Modified;
        }

        public async Task<bool> UserExistsAsync(Guid id)
        {
            return await _userDbContext.User.AnyAsync(u => u.UserId == id);
        }

        public async Task<bool> UserExistsByUserNameAsync(string userName)
        {
            return await _userDbContext.User.AnyAsync(u => u.UserName == userName);
        }
    }
}
